//
//  InputViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/3.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "InputViewController.h"
#import "BankView.h"
#import "InputScanViewController.h"
#import "ScreentoneViewController.h"

@interface InputViewController ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UITextViewDelegate>
{
    BankView *_disbgView;
    UIPickerView*_dispickerView;
    BankView *_bankBgView;
    UIPickerView*_bankPickerView;
}
@property(nonatomic,strong)CTextField *phoneText;
@property(nonatomic,strong)CTextField *nameText;
@property(nonatomic,strong)CTextField *equipmentText;
@property(nonatomic,strong)UILabel *clientLab;
@property(nonatomic,strong)NSString *clientUiud;
@property(nonatomic, strong)NSString *typeStr;
@property(nonatomic,strong)UILabel *instLab;
@property(nonatomic,strong)UITextView *detailText;
@property(nonatomic,strong)UILabel *distributionLab;//分配
@property(nonatomic,strong)NSString *distributionString;
@property(nonatomic,strong)NSMutableArray *distributionAr;
@property(nonatomic,strong)NSMutableArray *bankAr;
@property(nonatomic,strong)UILabel *equipmetNameLab;
@property(nonatomic, strong)NSString*instUuid;//机构uuid
@property(nonatomic, strong)NSString*machineUuid;//设备uuid
@property(nonatomic, strong)NSString*nameUuid;//师傅uuid
@property(nonatomic, strong)NSString*requstState;

@end

@implementation InputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _distributionAr=[[NSMutableArray alloc]init];
    
    _bankAr= [[NSMutableArray alloc] init];
    self.requstState=@"";

    //请求客户
    [CClass HttpRequestTask:YES withUrl:@"select_showBankSelect" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
        
        _bankAr =(NSMutableArray*)model.B_data ;
        [_bankPickerView reloadAllComponents];
        
    } onError:^(BaseModel *model) {
        
    }];


    //分配
    [CClass HttpRequestTask:YES withUrl:@"customer_findProInSameCity" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
        _distributionAr =(NSMutableArray*)[model.B_data objectForKey:@"list"];
        [_dispickerView reloadAllComponents];
    } onError:^(BaseModel *model) {
        
    }];
    
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    [narView addSubview:backView];
    __block typeof(self) blockself = self;
    [backView bk_whenTapped:^{
        [blockself.view endEditing:YES];
        [blockself backController];
        
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 50 - imgBack.frame.size.width,44);
    
    [backTitle setText:@"返回"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    
    UILabel *titleLab =[[UILabel alloc]init];
    [narView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(narView);
        make.top.equalTo(narView).offset(30);
    }];
    titleLab.font =F(16);
    titleLab.textColor =FontColor;
    titleLab.text=@"录入报修";
    UIView *sendView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-100, 0,100, 64)];
    [narView addSubview:sendView];
    UILabel *sendLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 85, 44)];
    sendLab.text=@"发送";
    [sendLab setFont:F(14)];

    sendLab.textAlignment=NSTextAlignmentRight;
    sendLab.textColor=MainColor;
    sendView.userInteractionEnabled=YES;
    [sendView addSubview:sendLab];
    sendLab.userInteractionEnabled=YES;
    [sendView bk_whenTapped:^{
       
        if ([_phoneText.text isEqualToString:@""]) {
            [CClass showMessage:@"请输入报修人电话！"];
            return ;
        }
        if ([_nameText.text isEqualToString:@""]) {
            [CClass showMessage:@"请输入报修人姓名！"];
            return ;
        }
        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
        if ([self.equipmentText.text isEqualToString:@""]) {//没有编译
            
            
        }else if(_equipmentText.text.length<16||_equipmentText.text.length>20)
        {
            [CClass showMessage:@"报修设备编码错误，请修改或删除！"];
            return;
            
        }else
        {
            if([self.requstState isEqualToString:@"1"])//填写的对
            {
                [dic setObject:_machineUuid forKey:@"machine_uuid"];
                
            }else if([self.requstState isEqualToString:@"0"])//填写不对
            {
                [CClass showMessage:@"报修设备编码错误，请修改或删除！"];
                return;
            }
        }
        
        if ([_clientLab.text isEqualToString:@"请选择"]) {
            [CClass showMessage:@"请选择报修客户！"];
            return ;
        }
        
        if ([_instLab.text isEqualToString:@"请选择"]) {
            [CClass showMessage:@"请选择报修机构！"];
            return ;
        }
        
        if ([_detailText.text isEqualToString:@""]||[_detailText.text isEqualToString:@"输入故障内容"]) {
            [CClass showMessage:@"请描述故障！"];
            return ;
        }
        [dic setObject:_phoneText.value forKey:@"mobile"];
        [dic setObject:_nameText.text forKey:@"name"];
        [dic setObject:_instUuid forKey:@"inst_uuid"];
        [dic setObject:_nameUuid forKey:@"pro_uuid"];
        [dic setObject:_detailText.text forKey:@"describe"];
        [self setIsLoading:YES];
        sendView.userInteractionEnabled=NO;
        [CClass HttpRequestTask:YES withUrl:@"warning_createByPro" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
            [self setIsLoading:NO];
            
            [CClass showLittleTimeMessage:@"录入成功" completion:^{
               [self backController];
            }];
        } onError:^(BaseModel *model) {
            sendView.userInteractionEnabled=YES;
        }];

    }];

   
  
    TPKeyboardAvoidingScrollView *bgScrollView=[[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74)];
    bgScrollView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgScrollView];
    _disbgView =[[BankView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)witnOriginY:WindowHeight/3 withTitle:@"分配"];
    _disbgView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.4];
    [self.view bringSubviewToFront:_disbgView];
    [self.view addSubview:_disbgView];
    _disbgView.hidden=YES;
    _dispickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, WindowWidth, WindowHeight/3-40)];
    _dispickerView.delegate=self;
    _dispickerView.dataSource=self;
    _dispickerView.backgroundColor =[UIColor whiteColor];
    _dispickerView.showsSelectionIndicator=YES;
    [_disbgView.whiteView addSubview:_dispickerView];
    [_disbgView setSubmitBtnPress:^{
        blockself.distributionLab.text=blockself.distributionString;
        
    }];
    _bankBgView =[[BankView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)witnOriginY:WindowHeight/3 withTitle:@"报修客户"];
    _bankBgView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.4];
    [self.view bringSubviewToFront:_bankBgView];
    [self.view addSubview:_bankBgView];
    _bankBgView.hidden=YES;
    _bankPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, WindowWidth, WindowHeight/3-40)];
    _bankPickerView.delegate=self;
    _bankPickerView.dataSource=self;
    _bankPickerView.backgroundColor =[UIColor whiteColor];
    _bankPickerView.showsSelectionIndicator=YES;
    [_bankBgView.whiteView addSubview:_bankPickerView];
    [_bankBgView setSubmitBtnPress:^{
        blockself.clientLab.text=blockself.typeStr;
        blockself.clientLab.textColor=[UIColor blackColor];
        
    }];
    UILabel *line =[[UILabel alloc]initWithFrame:CGRectMake(0,0, WindowWidth, 0.5)];
    line.backgroundColor=LineColor;
    [bgScrollView addSubview:line];
#pragma mark 联系电话
    UILabel *phoneLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line.frame), 65, 44)];
    phoneLab.font=F(13);
    phoneLab.textColor=FontColor;
    phoneLab.text=@"联系电话";
    [bgScrollView addSubview:phoneLab];
    UILabel *startLab1=[[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(line.frame), 18, 44)];
    startLab1.font=F(14);
    startLab1.textColor=MainColor;
    startLab1.text=@"*";
    [bgScrollView addSubview:startLab1];
    _phoneText =[[CTextField alloc]initWithFrame:CGRectMake(80+18, CGRectGetMaxY(line.frame), WindowWidth-80-18-15, 44)];
    _phoneText.font =F(12);
//    _phoneText.keyboardType=UIKeyboardTypeNumberPad;
    _phoneText.inputType=CTextFieldInputTypePhone;
    _phoneText.delegate=self;
    [_phoneText setNullMsg:@"请输入11位手机号!"];
    _phoneText.textAlignment=NSTextAlignmentRight;
    _phoneText.returnKeyType = UIReturnKeyNext;
    _phoneText.placeholder=@"输入报修人电话";
    _phoneText.textColor=[UIColor blackColor];
    [bgScrollView addSubview:_phoneText];
    UILabel *line1 =[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(_phoneText.frame), WindowWidth-15, 0.5)];
    line1.backgroundColor=LineColor;
    [bgScrollView addSubview:line1];
#pragma mark 报修人
    UILabel *nameLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line1.frame), 65, 44)];
    nameLab.font=F(13);
    nameLab.textColor=FontColor;
    nameLab.text=@"报修人";
    [bgScrollView addSubview:nameLab];
    UILabel *startLab2=[[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(line1.frame), 18, 44)];
    startLab2.font=F(14);
    startLab2.textColor=MainColor;
    startLab2.text=@"*";
    [bgScrollView addSubview:startLab2];

    _nameText =[[CTextField alloc]initWithFrame:CGRectMake(80+18, CGRectGetMaxY(line1.frame), WindowWidth-80-18-15, 44)];
    _nameText.font =F(12);
    [_nameText setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *tfd, NSRange range, NSString *str) {
        if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
            return NO;
        }
        return YES;
    }];
    [_nameText bk_addEventHandler:^(id sender) {
        
        UITextPosition *position = [_nameText positionFromPosition:[_nameText markedTextRange] offset:0];
        if (position) {
            return;
        }
        NSInteger maxLength = 20;
        if (maxLength > 0) {
            NSInteger splitcount;
            if (splitcount > 0) {
                maxLength = maxLength + maxLength /splitcount - (maxLength % splitcount == 0 ? 1 : 0);
            }
            if (_nameText.text.length > maxLength) {
                _nameText.text = [_nameText.text substringWithRange:NSMakeRange(0, maxLength)];
            }
            //移除表情
            NSString *newStr= [CClass disable_emoji:_nameText.text];
            _nameText.text=newStr;
            
            
        }
        
    } forControlEvents:UIControlEventEditingChanged];
    _nameText.inputType=CTextFieldInputTypeDefault;
    _nameText.delegate=self;
    _nameText.textAlignment=NSTextAlignmentRight;
    _nameText.returnKeyType = UIReturnKeyNext;
    _nameText.placeholder=@"输入报修人姓名";
    _nameText.textColor=[UIColor blackColor];
    [bgScrollView addSubview:_nameText];
    UILabel *line2 =[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(_nameText.frame), WindowWidth-15, 0.5)];
    line2.backgroundColor=LineColor;
    [bgScrollView addSubview:line2];
#pragma mark 报修设备
    UILabel *equipmetLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line2.frame), 65, 44)];
    equipmetLab.font=F(13);
    equipmetLab.textColor=FontColor;
    equipmetLab.text=@"报修设备";
    [bgScrollView addSubview:equipmetLab];
    UIImage *scanImg=[UIImage imageNamed:@"Ico_input_scan"];
    UIImageView *equipmetView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(equipmetLab.frame), CGRectGetMaxY(line2.frame)+10, scanImg.size.width, scanImg.size.height)];
    equipmetView.image=scanImg;
    equipmetView.userInteractionEnabled=YES;
    [equipmetView bk_whenTapped:^{
        //扫二维码
        InputScanViewController *inputScanVC=[[InputScanViewController alloc]init];
        [inputScanVC setBackInfo:^(NSMutableDictionary *dic) {
            //名字
            _equipmentText.text=[dic objectForKey:@"code"];
            self.requstState=@"1";
            _clientLab.text=[dic objectForKey:@"client"];
            _clientLab.textColor=[UIColor blackColor];
            _instLab.text=[dic objectForKey:@"instName"];
            _instLab.textColor=[UIColor blackColor];
            _instUuid=[dic objectForKey:@"inst"];
            _equipmetNameLab.text=[NSString stringWithFormat:@"%@  %@",[dic objectForKey:@"typeName"],[dic objectForKey:@"name"]];
            _equipmetNameLab.textColor=[UIColor blackColor];
            _machineUuid=[dic objectForKey:@"uuid"];

           
        }];
        [self pushController:inputScanVC];
    }];
    [bgScrollView addSubview:equipmetView];

    _equipmentText =[[CTextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(equipmetView.frame), CGRectGetMaxY(line2.frame), WindowWidth-CGRectGetMaxX(equipmetView.frame)-15, 44)];
    _equipmentText.font =F(12);
    _equipmentText.inputType=CTextFieldInputTypeNum;
    _equipmentText.delegate=self;
    _equipmentText.textAlignment=NSTextAlignmentRight;
    _equipmentText.returnKeyType = UIReturnKeyNext;
    _equipmentText.placeholder=@"输入报修设备编码";
    _equipmentText.textColor=[UIColor blackColor];
    [bgScrollView addSubview:_equipmentText];
    
    _equipmetNameLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_equipmentText.frame), WindowWidth-30, 44)];
    _equipmetNameLab.font=F(13);
    _equipmetNameLab.textColor=MainColor;
    _equipmetNameLab.textAlignment=NSTextAlignmentRight;
    _equipmetNameLab.text=@"未添加设备";
    [bgScrollView addSubview:_equipmetNameLab];
    UILabel *line3 =[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(_equipmetNameLab.frame), WindowWidth-15, 0.5)];
    line3.backgroundColor=LineColor;
    [bgScrollView addSubview:line3];

#pragma mark 报修客户
    UILabel *clientLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line3.frame), 65, 44)];
    clientLab.font=F(13);
    clientLab.textColor=FontColor;
    clientLab.text=@"报修客户";
    [bgScrollView addSubview:clientLab];
    UILabel *startLab4=[[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(line3.frame), 18, 44)];
    startLab4.font=F(14);
    startLab4.text=@"*";
    startLab4.textColor=MainColor;
    [bgScrollView addSubview:startLab4];
    _clientLab =[[UILabel alloc]initWithFrame:CGRectMake(80+18, CGRectGetMaxY(line3.frame), WindowWidth-80-18-15, 44)];
    _clientLab.font =F(12);
    _clientLab.textAlignment=NSTextAlignmentRight;
    _clientLab.text=@"请选择";
    _clientLab.textColor=C(@"c6c6cc");
    _clientLab.userInteractionEnabled=YES;
    [_clientLab bk_whenTapped:^{
        [self.view endEditing:YES];
        _bankBgView.hidden=NO;
        
        int i = 0;
        for (NSDictionary *province in _bankAr) {
            if ([province[@"name"] isEqualToString:_clientLab.text]) {
                [_bankPickerView selectRow:i inComponent:0 animated:YES];
                break;
            }
            i ++;
        }
        if (i == _bankAr.count) {
            NSDictionary *dic =[_bankAr objectAtIndex:0];
            _typeStr=[dic objectForKey:@"name"];
            _clientUiud =[dic objectForKey:@"uuid"];
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            _bankBgView.whiteView.frame=CGRectMake(0,WindowHeight/3*2 , WindowWidth, WindowHeight/3);
        } completion:nil];
        
    }];
    [bgScrollView addSubview:_clientLab];
    UILabel *line4 =[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(_clientLab.frame), WindowWidth-15, 0.5)];
    line4.backgroundColor=LineColor;
    [bgScrollView addSubview:line4];

#pragma mark 报修机构
    UILabel *mechanismLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line4.frame), 65, 44)];
    mechanismLab.font=F(13);
    mechanismLab.textColor=FontColor;
    mechanismLab.text=@"报修机构";
    [bgScrollView addSubview:mechanismLab];
    UILabel *startLab6=[[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(line4.frame), 18, 44)];
    startLab6.font=F(14);
    startLab6.text=@"*";
    startLab6.textColor=MainColor;
    [bgScrollView addSubview:startLab6];
    _instLab =[[UILabel alloc]initWithFrame:CGRectMake(80+18, CGRectGetMaxY(line4.frame), WindowWidth-80-18-15, 44)];
    _instLab.font =F(12);
    _instLab.textAlignment=NSTextAlignmentRight;
    _instLab.text=@"请选择";
    _instLab.textColor=C(@"c6c6cc");
    _instLab.userInteractionEnabled=YES;
    [_instLab bk_whenTapped:^{
        ScreentoneViewController *screenVC=[[ScreentoneViewController alloc]init];
        screenVC.backuiud=^(NSMutableDictionary*dic){
            _instLab.text =[dic objectForKey:@"name"];
            _instLab.textColor=[UIColor blackColor];
            //机构的id
            _instUuid =[dic objectForKey:@"uuid"];
            
        };
        [self pushController:screenVC];

    }];
    [bgScrollView addSubview:_instLab];
    UILabel *line6 =[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(_instLab.frame), WindowWidth-15, 0.5)];
    line6.backgroundColor=LineColor;
    [bgScrollView addSubview:line6];

#pragma mark 故障描述
    UILabel *detailLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line6.frame), 65, 44)];
    detailLab.font=F(13);
    detailLab.textColor=FontColor;
    detailLab.text=@"故障描述";
    [bgScrollView addSubview:detailLab];
    UILabel *startLab7=[[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(line6.frame), 18, 44)];
    startLab7.font=F(14);
    startLab7.text=@"*";
    startLab7.textColor=MainColor;
    [bgScrollView addSubview:startLab7];
    _detailText =[[UITextView alloc]initWithFrame:CGRectMake(80+18, CGRectGetMaxY(line6.frame), WindowWidth-80-18-15, 44)];
    _detailText.font =F(12);
    _detailText.delegate=self;
    _detailText.textAlignment=NSTextAlignmentRight;
    _detailText.returnKeyType = UIReturnKeyNext;
    _detailText.text=@"输入故障内容";
    _detailText.delegate=self;
    _detailText.textColor=C(@"c6c6cc");
    [bgScrollView addSubview:_detailText];
    UILabel *line7=[[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(_detailText.frame), WindowWidth-15, 0.5)];
    line7.backgroundColor=LineColor;
    [bgScrollView addSubview:line7];
#pragma mark 分配给
    UILabel *distributionLab=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line7.frame), 65, 44)];
    distributionLab.font=F(13);
    distributionLab.textColor=FontColor;
    distributionLab.text=@"分配给";
    [bgScrollView addSubview:distributionLab];
    UILabel *startLab8=[[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(line7.frame), 18, 44)];
    startLab8.font=F(14);
    startLab8.text=@"*";
    startLab8.textColor=MainColor;
    [bgScrollView addSubview:startLab8];
    _distributionLab =[[UILabel alloc]initWithFrame:CGRectMake(80+18, CGRectGetMaxY(line7.frame), WindowWidth-80-18-15, 44)];
    _distributionLab.font =F(12);
    _distributionLab.textAlignment=NSTextAlignmentRight;
    _distributionLab.textColor=[UIColor blackColor];
    [bgScrollView addSubview:_distributionLab];
    _distributionLab.text=[CClass getCacheData:NSUD_Name];
    _nameUuid=[CClass getCacheData:NSUD_UUID];
    _distributionLab.userInteractionEnabled=YES;
    [_distributionLab bk_whenTapped:^{
        [self.view endEditing:YES];
        _disbgView.hidden=NO;
        int i = 0;
        for (NSDictionary *province in _distributionAr) {
            if ([province[@"name"] isEqualToString:_distributionLab.text]) {
                [_dispickerView selectRow:i inComponent:0 animated:YES];
                break;
            }
            i ++;
        }
        if (i == _distributionAr.count) {
            NSDictionary *dic =[_distributionAr objectAtIndex:0];
            _distributionString=[dic objectForKey:@"name"];
            _nameUuid =[dic objectForKey:@"uuid"];
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            _disbgView.whiteView.frame=CGRectMake(0,WindowHeight/3*2 , WindowWidth, WindowHeight/3);
        } completion:nil];
    }];
     


    UILabel *line8=[[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_distributionLab.frame), WindowWidth, 0.5)];
    line8.backgroundColor=LineColor;
    [bgScrollView addSubview:line8];
    bgScrollView.contentSize=CGSizeMake(WindowWidth, CGRectGetMaxY(line8.frame));
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField==_phoneText) {

        if (!_phoneText.verfiy) {
            return;
        }
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *shouldBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField==_phoneText) {
        if (shouldBeString.length==13) {
            NSString *v = shouldBeString;
         
            v = [shouldBeString stringByReplacingOccurrencesOfString:@" " withString:@""];
            
            //通过电话发送请求找到姓名 还没有判断注册没有
            [CClass HttpRequestTask:YES withUrl:@"customer_findUser" withData:@{@"mobile":v} notifyError:YES onSuccess:^(BaseModel *model) {
                if ([[model.B_data allKeys] containsObject:@"name"]) {
                    _nameText.userInteractionEnabled=NO;
                    _nameText.text=[model.B_data objectForKey:@"name"];
                    [self.view endEditing:YES];
                    
                }else
                {
                    _nameText.text=@"";
                    _nameText.userInteractionEnabled=YES;

                    
                }
                
                
            } onError:^(BaseModel *model) {
                _nameText.userInteractionEnabled=YES;

            }];
            
        }else
        {
            
        }
   
        
    }
    else if (textField==_equipmentText) {
        // 获得需要进行搜索的关键字
        if(shouldBeString.length<=20)
        {
            if (shouldBeString.length<=20&&shouldBeString.length>=16 ) {
                
                //通过设备编号发送请求找到对应设备
                
                [CClass HttpRequestTask:YES withUrl:@"machine_detail" withData:@{@"code":shouldBeString} notifyError:YES onSuccess:^(BaseModel *model) {
                    _clientLab.text=[model.B_data objectForKey:@"bank_name"];
                    _clientLab.textColor=[UIColor blackColor];
                    _instLab.text=[model.B_data  objectForKey:@"inst_name"];
                    _instLab.textColor=[UIColor blackColor];
                    _instUuid=[model.B_data  objectForKey:@"inst"];
                    _equipmetNameLab.textColor=[UIColor blackColor];
                    _equipmetNameLab.text=[NSString stringWithFormat:@"%@  %@",[model.B_data  objectForKey:@"type_name"],[model.B_data  objectForKey:@"name"]];
                    _machineUuid=[model.B_data objectForKey:@"uuid"];
                    [self.view endEditing:YES];
                    self.requstState=@"1";

                } onError:^(BaseModel *model) {
                    _clientLab.text=@"";
                    _clientLab.textColor=C(@"c6c6c6");
                    _instUuid=@"";
                    _instLab.text=@"请选择";
                    _instLab.textColor=C(@"c6c6cc");
                    _equipmetNameLab.textColor=MainColor;
                    _equipmetNameLab.text=@"未添加设备";
                    _machineUuid=@"";
                    self.requstState=@"0";

                    
                }];
            }else
            {
                _clientLab.text=@"";
                _clientLab.textColor=C(@"c6c6c6");
                _instUuid=@"";
                _instLab.text=@"请选择";
                _instLab.textColor=C(@"c6c6cc");
                _equipmetNameLab.textColor=MainColor;
                _equipmetNameLab.text=@"未添加设备";
                _machineUuid=@"";
                self.requstState=@"0";

            }
            return YES;
        }else if(shouldBeString.length>20)
        {
            
            return NO;
        }
       

    }
    
    
    return YES;
    
}
#pragma mark 输入框的限制

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if(textView==_detailText)
    {
        if ([textView.text isEqualToString:@"输入故障内容"]) {
            [textView setText:@""];
            [textView setTextColor:[UIColor blackColor]];
        }
    }
    
}
- (void)textViewDidChange:(UITextView *)textView{
    //移除表情
    if ([CClass stringContainsEmoji:textView.text]) {
        textView.text=[textView.text substringToIndex:textView.text.length-2];
        return;
    }
    if(textView==_detailText)
    {
        if ([CClass stringContainsEmoji:textView.text]) {
            textView.text=[textView.text substringToIndex:textView.text.length-2];
            return;
        }
        if (textView.markedTextRange == nil && textView.text.length > 20)  //加上 textView.markedTextRange == nil判断，当此属性为nil时，代表不在这种联想输入等待确定状态。
            
        {
            
            textView.text = [textView.text substringToIndex:20];
            
        }
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }

    
    return YES;
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView==_dispickerView) {
        return _distributionAr.count;

    }else
    {
        return _bankAr.count;

    }
    
  
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView==_dispickerView) {
        NSDictionary *dic =[_distributionAr objectAtIndex:row];
        return [dic objectForKey:@"name"];
    }else
    {
        NSDictionary *dic =[_bankAr objectAtIndex:row];
        return [dic objectForKey:@"name"];
    }
    
        
   

}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView==_dispickerView) {
        NSDictionary *dic =[_distributionAr objectAtIndex:row];
        self.distributionString=[dic objectForKey:@"name"];
        _nameUuid=[dic objectForKey:@"uuid"];
    }else
    {
        NSDictionary *dic =[_bankAr objectAtIndex:row];
        _typeStr=[dic objectForKey:@"name"];
        _clientUiud =[dic objectForKey:@"uuid"];
    }
 
    
}


@end
